﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHelper;
using JsonHelper = MyHelper.JsonHelper;
using clf.weighing.entity;

namespace weighing
{
    public class ConfigDao : BaseDao<Config>
    {

        #region instance

        private static ConfigDao? instance;
        public static ConfigDao Instance => instance ??= new ConfigDao();

        public override string GetTableName()
        {
            return TableName.config.ToString();
        }
        #endregion


        /// <summary>
        /// 获取本地的配制项
        /// </summary>
        /// <returns>第一条</returns>
        public LocalConfig GetLocalConfig()
        {
            string condition = $"{ConfigField.client_id} = {App.InitConfig.clientId}";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, null, 1, 0);
            var data = Sqlite.Select<Config>(sql);
            if (data == null || data.Count <= 0)
            {
                CommonFunction.SaveAppConfig(LocalConfig.Default);
                return LocalConfig.Default;
            }
            else
            {
                return LocalConfig.FromDbConfig(data[0]);
            }
        }

        /// <summary>
        /// 查找 本地配制
        /// </summary>
        /// <returns></returns>
        public Config? FindLocal()
        {
            string condition = $"{ConfigField.client_id} = {App.InitConfig.clientId}";
            String sql = SqlBuilder.GetSelectSql(GetTableName(), null, condition, null, null, null, 1, 0);
            var data = Sqlite.Select<Config>(sql);
            return data.FirstOrDefault();
        }
        public int UpdateAppConfig(string configName)
        {
            if (configName.IsNullOrEmpty())
            {
                return LocalConfigDao.Instance.InsertOrUpdate(App.Config);
            }
            if (configName == nameof(WeighingConfig))
            {
                string con = JsonHelper.ObjectToJson(App.Config.WeighingConfig);
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    weighingConfig = con
                };
                return Sqlite.Update(config);
            }

            if (configName == nameof(FunctionConfig))
            {
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    functionConfig = JsonHelper.ObjectToJson(App.Config.FunctionConfig)
                };
                return Sqlite.Update(config);
            }

            if (configName == nameof(SystemConfig))
            {
               
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    systemConfig = JsonHelper.ObjectToJson(App.Config.SystemConfig)
                };
                return Sqlite.Update(config);
            }

            if (configName == nameof(ImgConfig))
            {
                ImgConfig ic = App.Config.ImgConfig;
                ic.CapturePath = UrlHelper.Encode(ic.CapturePath);
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    imgConfig = JsonHelper.ObjectToJson(ic)
                };
                return Sqlite.Update(config);
            }

            if (configName == nameof(UserConfig))
            {
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    userConfig = JsonHelper.ObjectToJson(App.Config.UserConfig)
                };
                return Sqlite.Update(config);
            }

            if (configName == nameof(BillPrintConfig))
            {
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    billPrintConfig = JsonHelper.ObjectToJson(App.Config.BillPrintConfig)
                };
                return Sqlite.Update(config);
            }

      

            if (configName == nameof(OtherConfig))
            {
                var config = new Config()
                {
                    id = (long)App.Config.Id,
                    clientId = App.Config.ClientId,
                    otherConfig = JsonHelper.ObjectToJson(App.Config.OtherConfig)
                };
                return Sqlite.Update(config);
            }

            return 0;
        }
    }
}
